﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Janus.Windows.GridEX;
using YiLe.BLL.Service;
using YiLe.DataForms;
using YiLe.Model.Domain;

namespace YiLe.UI
{
    public partial class CustomersGridEXView
    {
        protected override void OnFolderViewChanged()
        {
            base.OnFolderViewChanged();
            GridEXLayout layout = this.GridEX.FindLayout(this.FolderView.LayoutName);
            //change datasource from application context, not from mDataProvide.mDataSet; modify by cy
            //layout.SetDataBinding(this.DataSource, "Customers");
            IUserService userService = Context["UserService"] as IUserService;
            IList<User> userList = userService.GetAll();
            layout.SetDataBinding(userList, "Users");

            this.GridEX.CurrentLayout = layout;
            //this.GridEX.DataSource = userList;
            this.FillCountriesValueList();
            //We need to call Refetch method if valuelist or dropdowns are populated
            //after the datasource in the grid is populated
            this.GridEX.Refetch();

            this.EnableCommands();
        }

        private void FillCountriesValueList()
        {
            //TODO:YiLeSystemApp.YiLeSystemApplication.GetCountriesTable()
/*
            DataView countries = new DataView(YiLeSystemApp.YiLeSystemApplication.GetCountriesTable());
            this.GridEX.RootTable.Columns["Country"].HasValueList = true;
            this.GridEX.RootTable.Columns["Country"].EditType = EditType.Combo;
            this.GridEX.RootTable.Columns["Country"].ColumnType = ColumnType.ImageAndText;
            this.GridEX.RootTable.Columns["Country"].ValueList.PopulateValueList(countries, "Country", "Country", "Flag", Color.Magenta, new Size(19, 13));
        */}

        public override void Edit()
        {
            foreach (GridEXSelectedItem item in this.GridEX.SelectedItems)
            {
                if (item.RowType == RowType.Record)
                {
                    CustomerDataForm custDialog = new CustomerDataForm();
                    custDialog.Show();
                    //TODO: item.GetRow().DataRow is User, can cast DataRowView
                    //custDialog.Edit(((DataRowView)(item.GetRow().DataRow)).Row);
                }
            }
        }
    }
}
